package com.leo.tool.poi;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Slf4j
public class PoiTest {
    public static void main(String[] args) throws IOException, InvalidFormatException {
        Path dir = Path.of("poi/target/classes").toRealPath();
        DataFormatter dataFormatter = new DataFormatter();

        try (XSSFWorkbook sheets = new XSSFWorkbook(dir.resolve("test.xlsx").toFile())) {
            XSSFCell cell = sheets.getSheetAt(0).getRow(0).getCell(0); // 底层为double
            log.info(dataFormatter.formatCellValue(cell));
        }

        log.info("tmpdir:{}.", System.getProperty("java.io.tmpdir") + "poifiles");
        try (SXSSFWorkbook sheets = new SXSSFWorkbook(2)) {
            sheets.setCompressTempFiles(true);
            SXSSFSheet sheet = sheets.createSheet();
            sheet.createRow(0).createCell(0);
            sheet.createRow(1).createCell(1);
            sheet.createRow(2).createCell(2);
            sheet.createRow(3).createCell(3);
            sheets.write(Files.newOutputStream(dir.resolve("new.xlsx")));
//            sheets.dispose();
        }
    }
}
